classdef email_notification < handle
  % General set-up for and execution of automated email notification
  % Derived from handle
  % Constructor signature: oEN = error_notification(recipients), arg optional
  %
  % Author: David Goldsmith, Wash. State Dept. of Ecology, dgol461@ecy.wa.gov
  % Release date: 2012-05
    
    properties
        mail_recip = {}; 
        mail_sender = 'dgol461@ecy.wa.gov';
        msg = {};
        outgoingMailServ = '165.151.24.51';
        send = 1;
    end
    
    methods
        function oEN = email_notification(recipients)
            if nargin
                setpref('Internet', 'SMTP_Server', oEN.outgoingMailServ);
                setpref('Internet', 'E_mail', oEN.mail_sender);
                oEN.mail_recip = recipients;
            end
        end
        
        function appendMsg(obj, msg)
            obj.msg{end+1} = msg;
        end
    
        function notify(obj, app, recipients, att)
            if nargin < 4
                att = '';
            end
            if nargin < 3
                recipients = obj.mail_recip;
            end
            
            if isempty(att)
                sendmail(recipients, [app ' email notification'],...
                         regexprep(obj.msg, '.  ', ['.' char(10)]));
            else
                sendmail(recipients, [app ' email notification'],...
                         regexprep(obj.msg, '.  ', ['.' char(10)]),...
                         att);
            end
        end
        
    end
        
end

